01. Introduction

Introduction to Test Doubles, Mocking, and Integration Testing

ND079 JPND C3 L5 A01 Advanced Testing Topics

Testing Tools So Far:

  • Assertions
  • RepeatedTest
  • ParameterizedTest
  • Code Coverage

These tools are great for testing that a method meets its requirements, but can run into problems if those methods rely on other methods. In this unit, we'll look at tools for testing classes that rely on additional dependencies to perform complex actions.

Classes With Dependencies

In the last lesson, we looked at testing a fizzbuzz method. What if we want to test another class that uses that fizzBuzz method. Let's move fizzBuzz into a new class called SalesService and then reference it from inside another class.

public class UserService {
   SalesService salesService = new SalesService();

   public String fancyBusiness(int n) {
       String result = salesService.fizzBuzz(n); //gets the fizzBuzz value
       switch(result) { //then does something else with it
           case "Fizz":
           case "Buzz":
           case "FizzBuzz":
               return result;
           default:
               return Integer.parseInt(result) % 2 == 0 ? "Baz" : "Cat"
       }
   }
   // remainder omitted
}

When testing the fancyBusiness method, we need a way to check requirements like:

  1. If SalesService.fizzBuzz returns “Fizz”, “Buzz”, or “FizzBuzz”, **then **fancyBusiness just returns that string.
  2. **If **SalesService.fizzBuzz returns an even number, **then **fancyBusiness returns “Baz”.
  3. **If **SalesService.fizzBuzz returns an odd number, **then **fancyBusiness returns “Cat”.